home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / vector / FloatVect.c < prev    next >
C/C++ Source or Header  |  1990-05-16  |  2KB  |  91 lines

  1. /* FloatVect.c -- Data type-specific functions for class FloatVec
  2.  
  3. Author:
  4.     Tom Keffer
  5.     School of Oceanography, WB-10
  6.     Univ. of Washington
  7.     Seattle, WA 98195
  8.     (206) 543-6455
  9.  
  10.     Internet: keffer@sperm.ocean.washington.edu
  11.     uucp:     uw-beaver!sperm.ocean.washington.edu!keffer
  12.     BITNET:   keffer%sperm.ocean.washington.edu@UWAVM
  13.     Telemail: T.KEFFER/OMNET     
  14.  
  15. Copied and modified from Gorlen's DoubleVect.c.
  16.  
  17. Function:
  18.     
  19. Data type -specific functions for class FloatVec.
  20.  
  21. Modification History:
  22.  
  23. $Log:    FloatVect.c,v $
  24.  * Revision 3.0  90/05/16  23:00:32  kgorlen
  25.  * Release for 1st edition.
  26.  * 
  27. */
  28.  
  29. #include "FloatVec.h"
  30. #include "DoubleVec.h"
  31. #include "nihclconfig.h"
  32. #include <iomanip.h>
  33. #include <libc.h>
  34.  
  35. #define    THIS    FloatVec
  36. #define    BASE    Vector
  37.  
  38. // Conversion from double to float.  Added by tk.
  39. FloatVec::FloatVec(const DoubleVec& dv) :
  40.        BASE(dv.length())
  41. {
  42.   v = NULL;
  43.   if(n){
  44.     v = new float[n];
  45.     float* fp = v;
  46.     const double* dp = dv.pt();
  47.     unsigned i = n;
  48.     while(i--) *fp++ = float(*dp++);
  49.   }
  50. }
  51.  
  52. static int typeCmp(const void* a, const void* b)
  53. {
  54.     float t = (const float*)a-(const float*)b;
  55.     if (t < 0) return -1;
  56.     if (t > 0) return 1;
  57.     return 0;
  58. }
  59.  
  60. void THIS::sort()
  61. {
  62.     qsort(v,n,sizeof(float),typeCmp);
  63. }
  64.  
  65. unsigned THIS::hash() const
  66. {
  67.     unsigned h = n;
  68. #ifdef LOG2_INT
  69.     unsigned i = n*sizeof(float) >> LOG2_INT;
  70. #else
  71.     unsigned i = n*sizeof(float)/sizeof(int);
  72. #endif
  73.     unsigned* vv = (unsigned*)v;
  74.     while (i--) h ^= *vv++;
  75.     return h;
  76. }
  77.  
  78. void THIS::printOn(ostream& strm) const
  79. {
  80.     for (unsigned i=0; i<n; i++) {
  81.         if (i>0 && (i%6 == 0)) strm << "\n\t";
  82.         strm << setw(13) << setprecision(5) << v[i];
  83.     }
  84. }
  85.  
  86. void THIS::scanFrom(istream& strm)
  87. {
  88.     extern const int NIHCL_NYET;
  89.     seterror(NIHCL_NYET,DEFAULT,className(),"scanFrom");
  90. }
  91.